package edu.gatech.cs2340.OregonTrail;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.util.Map;

/**
 * This is the panel that displays the traveling portion of the game.
 * 
 * @author Joshua Roberts
 * @version 1.0.1
 */
public class PlayPanel extends JPanel{

	private static final long serialVersionUID = 1L;
	//private OregonTrailApp parentFrame;
	private Image backgroundImage;
	private ePace pace;
	private eRation ration;
	//private Game game;
	private JLabel lblFoodRemain;
	private JLabel lblMiles;
	private JLabel lblMoneyRemain;
	private JLabel lblDays;

	
	
	public PlayPanel(final OregonTrailApp parentFrame, final Game game) throws IOException {
		//this.parentFrame = parentFrame;
		//this.game = game;
		backgroundImage = ImageIO.read(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/background.png"));

		setPreferredSize(new Dimension(524, 600));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{25, 86, 95, 80, 95, 95, 25, 0};
		gridBagLayout.rowHeights = new int[]{15, 30, 30, 162, 60, 60, 125, 25, 30, 30, 15, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblFood = new JLabel("Food: ");
		lblFood.setHorizontalAlignment(SwingConstants.RIGHT);
		lblFood.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblFood = new GridBagConstraints();
		gbc_lblFood.anchor = GridBagConstraints.WEST;
		gbc_lblFood.insets = new Insets(0, 0, 5, 5);
		gbc_lblFood.gridx = 1;
		gbc_lblFood.gridy = 1;
		add(lblFood, gbc_lblFood);
		
		lblFoodRemain = new JLabel();
		lblFoodRemain.setHorizontalAlignment(SwingConstants.LEFT);
		lblFoodRemain.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblFoodRemain = new GridBagConstraints();
		gbc_lblFoodRemain.anchor = GridBagConstraints.WEST;
		gbc_lblFoodRemain.insets = new Insets(0, 0, 5, 5);
		gbc_lblFoodRemain.gridx = 2;
		gbc_lblFoodRemain.gridy = 1;
		add(lblFoodRemain, gbc_lblFoodRemain);
		
		JLabel lblMilesTraveled = new JLabel("MilesTraveled:");
		lblMilesTraveled.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblMilesTraveled = new GridBagConstraints();
		gbc_lblMilesTraveled.anchor = GridBagConstraints.WEST;
		gbc_lblMilesTraveled.insets = new Insets(0, 0, 5, 5);
		gbc_lblMilesTraveled.gridx = 4;
		gbc_lblMilesTraveled.gridy = 1;
		add(lblMilesTraveled, gbc_lblMilesTraveled);
		
		lblMiles = new JLabel();
		lblMiles.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblMiles = new GridBagConstraints();
		gbc_lblMiles.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblMiles.insets = new Insets(0, 0, 5, 5);
		gbc_lblMiles.gridx = 5;
		gbc_lblMiles.gridy = 1;
		add(lblMiles, gbc_lblMiles);
		
		
		JLabel lblMoney = new JLabel("Money: ");
		lblMoney.setHorizontalAlignment(SwingConstants.RIGHT);
		lblMoney.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblMoney = new GridBagConstraints();
		gbc_lblMoney.anchor = GridBagConstraints.WEST;
		gbc_lblMoney.insets = new Insets(0, 0, 5, 5);
		gbc_lblMoney.gridx = 1;
		gbc_lblMoney.gridy = 2;
		add(lblMoney, gbc_lblMoney);
		
		lblMoneyRemain = new JLabel();
		lblMoneyRemain.setHorizontalAlignment(SwingConstants.LEFT);
		lblMoneyRemain.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblMoneyRemain = new GridBagConstraints();
		gbc_lblMoneyRemain.anchor = GridBagConstraints.WEST;
		gbc_lblMoneyRemain.insets = new Insets(0, 0, 5, 5);
		gbc_lblMoneyRemain.gridx = 2;
		gbc_lblMoneyRemain.gridy = 2;
		add(lblMoneyRemain, gbc_lblMoneyRemain);
		
		JLabel lblDaysTraveled = new JLabel("DaysTraveled:");
		lblDaysTraveled.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblDaysTraveled = new GridBagConstraints();
		gbc_lblDaysTraveled.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblDaysTraveled.insets = new Insets(0, 0, 5, 5);
		gbc_lblDaysTraveled.gridx = 4;
		gbc_lblDaysTraveled.gridy = 2;
		add(lblDaysTraveled, gbc_lblDaysTraveled);	
		
		lblDays = new JLabel(Integer.toString(game.getDay()));
		lblDays.setFont(new Font("Tahoma", Font.PLAIN, 14));
		GridBagConstraints gbc_lblDays = new GridBagConstraints();
		gbc_lblDays.anchor = GridBagConstraints.NORTH;
		gbc_lblDays.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblDays.insets = new Insets(0, 0, 5, 5);
		gbc_lblDays.gridx = 5;
		gbc_lblDays.gridy = 2;
		add(lblDays, gbc_lblDays);
			
		JButton btnPaceslow = new JButton("");
		GridBagConstraints gbc_btnPaceslow = new GridBagConstraints();
		gbc_btnPaceslow.anchor = GridBagConstraints.EAST;
		gbc_btnPaceslow.insets = new Insets(0, 0, 5, 5);
		gbc_btnPaceslow.gridx = 2;
		gbc_btnPaceslow.gridy = 4;
		add(btnPaceslow, gbc_btnPaceslow);	
		btnPaceslow.setIcon(new ImageIcon(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/paceslow.png")));
		btnPaceslow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pace = ePace.low;
				game.updatePace(pace);
			}
		});
		
		JButton btnPacenormal = new JButton("");
		GridBagConstraints gbc_btnPacenormal = new GridBagConstraints();
		gbc_btnPacenormal.insets = new Insets(0, 0, 5, 5);
		gbc_btnPacenormal.gridx = 3;
		gbc_btnPacenormal.gridy = 4;
		add(btnPacenormal, gbc_btnPacenormal);
		btnPacenormal.setIcon(new ImageIcon(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/pacenormal.png")));
		btnPacenormal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pace = ePace.medium;
				game.updatePace(pace);
			}
		});
		
		JButton btnPacefast = new JButton("");
		GridBagConstraints gbc_btnPacefast = new GridBagConstraints();
		gbc_btnPacefast.anchor = GridBagConstraints.WEST;
		gbc_btnPacefast.insets = new Insets(0, 0, 5, 5);
		gbc_btnPacefast.gridx = 4;
		gbc_btnPacefast.gridy = 4;
		add(btnPacefast, gbc_btnPacefast);
		btnPacefast.setIcon(new ImageIcon(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/pacefast.png")));
		btnPacefast.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pace = ePace.high;
				game.updatePace(pace);
			}
		});
		
		JButton btnRationLow = new JButton("");
		GridBagConstraints gbc_btnRationsLow = new GridBagConstraints();
		gbc_btnRationsLow.anchor = GridBagConstraints.EAST;
		gbc_btnRationsLow.insets = new Insets(0, 0, 5, 5);
		gbc_btnRationsLow.gridx = 2;
		gbc_btnRationsLow.gridy = 5;
		add(btnRationLow, gbc_btnRationsLow);
		btnRationLow.setIcon(new ImageIcon(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/rationslow.png")));
		btnRationLow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ration = eRation.low;
				game.updateRation(ration);
			}
		});
		
		JButton btnRationHigh = new JButton("");
		GridBagConstraints gbc_btnRationHigh = new GridBagConstraints();
		gbc_btnRationHigh.anchor = GridBagConstraints.WEST;
		gbc_btnRationHigh.insets = new Insets(0, 0, 5, 5);
		gbc_btnRationHigh.gridx = 4;
		gbc_btnRationHigh.gridy = 5;
		add(btnRationHigh, gbc_btnRationHigh);
		btnRationHigh.setIcon(new ImageIcon(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/rationfast.png")));
		btnRationHigh.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ration = eRation.high;
				game.updateRation(ration);
			}
		});
		
		JButton btnRationNormal = new JButton("");
		GridBagConstraints gbc_btnRationNormal = new GridBagConstraints();
		gbc_btnRationNormal.insets = new Insets(0, 0, 5, 5);
		gbc_btnRationNormal.gridx = 3;
		gbc_btnRationNormal.gridy = 5;
		add(btnRationNormal, gbc_btnRationNormal);
		btnRationNormal.setIcon(new ImageIcon(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/rationnormal.png")));
		btnRationNormal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ration = eRation.medium;
				game.updateRation(ration);
			}
		});

		JButton btnTakeaturn = new JButton("");
		GridBagConstraints gbc_btnTakeaturn = new GridBagConstraints();
		gbc_btnTakeaturn.insets = new Insets(0, 0, 5, 5);
		gbc_btnTakeaturn.gridwidth = 3;
		gbc_btnTakeaturn.gridx = 2;
		gbc_btnTakeaturn.gridy = 6;
		add(btnTakeaturn, gbc_btnTakeaturn);
		btnTakeaturn.setIcon(new ImageIcon(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/wagon.png")));
		btnTakeaturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				game.takeATurn(parentFrame.getPlayPanel());
				game.updatePlayPanel(parentFrame.getPlayPanel());
			}
		});
		
		JLabel lblTravelOn = new JLabel("Travel On");
		GridBagConstraints gbc_lblTravelOn = new GridBagConstraints();
		gbc_lblTravelOn.insets = new Insets(0, 0, 5, 5);
		gbc_lblTravelOn.gridx = 3;
		gbc_lblTravelOn.gridy = 7;
		add(lblTravelOn, gbc_lblTravelOn);
		
		JButton btnHunt = new JButton("Hunt");
		btnHunt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		
		JButton btnRest = new JButton("Rest");
		btnRest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		GridBagConstraints gbc_btnRest = new GridBagConstraints();
		gbc_btnRest.insets = new Insets(0, 0, 5, 5);
		gbc_btnRest.gridx = 2;
		gbc_btnRest.gridy = 8;
		add(btnRest, gbc_btnRest);
		GridBagConstraints gbc_btnHunt = new GridBagConstraints();
		gbc_btnHunt.insets = new Insets(0, 0, 5, 5);
		gbc_btnHunt.gridx = 3;
		gbc_btnHunt.gridy = 8;
		add(btnHunt, gbc_btnHunt);
		
		JButton btnTrade = new JButton("Trade");
		btnTrade.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		GridBagConstraints gbc_btnTrade = new GridBagConstraints();
		gbc_btnTrade.insets = new Insets(0, 0, 5, 5);
		gbc_btnTrade.gridx = 4;
		gbc_btnTrade.gridy = 8;
		add(btnTrade, gbc_btnTrade);
		
		JButton btnMap = new JButton("Map");
		btnMap.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				parentFrame.showMap();
			}
		});
		
		JButton btnMenu = new JButton("Menu");
		btnMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				parentFrame.showMenu();
			}
		});
		GridBagConstraints gbc_btnMenu = new GridBagConstraints();
		gbc_btnMenu.insets = new Insets(0, 0, 5, 5);
		gbc_btnMenu.gridx = 2;
		gbc_btnMenu.gridy = 9;
		add(btnMenu, gbc_btnMenu);
		GridBagConstraints gbc_btnMap = new GridBagConstraints();
		gbc_btnMap.insets = new Insets(0, 0, 5, 5);
		gbc_btnMap.gridx = 4;
		gbc_btnMap.gridy = 9;
		add(btnMap, gbc_btnMap);
	}
	
	public void updatePlayPanel(int days, int miles, int money,	Map<Item, Integer> stock) {
		Item food = new Item();
		food.setItemName("Food");
		lblFoodRemain.setText(Integer.toString(stock.get(food)));
		lblMiles.setText(Integer.toString(miles));
		lblMoneyRemain.setText(Integer.toString(money));
		lblDays.setText(Integer.toString(days));
	}

	public void paintComponent(Graphics g) {
	    super.paintComponent(g);

	    // Draw the background image.
	    g.drawImage(backgroundImage, 0, 0, null);
	}	
}
